package com.thread;

import java.util.concurrent.*;

/**
 * 创建线程的5种方式
 */
public class NewThreadWay {
    static class MyThread extends Thread {
        @Override
        public void run() {
            System.out.println("继承Thread类方式");
        }
    }

    static class MyRun implements Runnable {
        @Override
        public void run() {
            System.out.println("实现Runnable接口方式");
        }
    }

    static class MyCall implements Callable<String> {
        @Override
        public String call() throws Exception {
            System.out.println("实现Callable接口方式");
            return "success";
        }
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //方式1
        new MyThread().start();

        //方式2
        new Thread(new MyRun()).start();

        //方式3
        new Thread(() -> System.out.println("Lambda表达式方式")).start();

        //方式4
        ExecutorService es = Executors.newCachedThreadPool();
        es.execute(() -> System.out.println("线程池方式"));

        //带返回值方式
        //方式5.1
        Future<String> f = es.submit(new MyCall());
        String s = f.get();
        System.out.println(s);

        es.shutdown();

        //方式5.2
        FutureTask<String> ft = new FutureTask(new MyCall());
        new Thread(ft).start();
        String s1 = ft.get();
        System.out.println(s1);

    }
}
